package com.yx.frame.builder;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.yx.frame.builder.xml.MethodMencacheXml;

public class MencacheContent extends Composite {
	private Text Timeout;
	private List SrcParmlist;
	private List Keylist;
	private Button IsCached;
	private Button IsPrivate;
	private Button IsPagable;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MencacheContent(Composite parent, int style) {
		super(parent, style);

		SrcParmlist = new List(this, SWT.BORDER);
		SrcParmlist.setBounds(51, 78, 187, 214);

		Keylist = new List(this, SWT.BORDER);
		Keylist.setBounds(280, 78, 187, 214);

		Label lblkey = new Label(this, SWT.NONE);
		lblkey.setBounds(51, 55, 115, 17);
		lblkey.setText("数据缓存 KEY：");

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = SrcParmlist.getSelectionIndex();
				if(selectIdx<0||selectIdx>=SrcParmlist.getItemCount()){
					return;
				}
				String parm = SrcParmlist.getItem(selectIdx);
				String[] items = Keylist.getItems();
				boolean isExist = false;
				for (int i = 0; i < items.length; i++) {
					if (items[i].equals(parm)) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					Keylist.add(parm);
				}
			}
		});
		button.setBounds(244, 132, 28, 27);
		button.setText(">");

		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = Keylist.getSelectionIndex();
				if(selectIdx<0||selectIdx>=Keylist.getItemCount()){
					return;
				}
				if (selectIdx >= 0) {
					Keylist.remove(selectIdx);
				}
			}
		});
		button_1.setText("<");
		button_1.setBounds(244, 177, 28, 27);

		Label label = new Label(this, SWT.NONE);
		label.setBounds(216, 30, 60, 17);
		label.setText("内存驻留：");

		Timeout = new Text(this, SWT.BORDER | SWT.RIGHT);
		Timeout.setText("86400");
		Timeout.setBounds(280, 27, 169, 23);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBounds(452, 30, 21, 17);
		label_1.setText("秒");

		Label lblkeyid = new Label(this, SWT.WRAP);
		lblkeyid.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		lblkeyid.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblkeyid.setBounds(51, 307, 416, 55);
		lblkeyid.setText("说明：数据缓存KEY用于在缓存服务器唯一查找数据的唯一凭证，默认带本服务的ID，其他部分用户根据需要添加");

		IsCached = new Button(this, SWT.CHECK);
		IsCached.setBounds(51, 30, 93, 17);
		IsCached.setText("数据内存缓存");

		IsPrivate = new Button(this, SWT.CHECK);
		IsPrivate.setText("用户私有");
		IsPrivate.setBounds(280, 55, 69, 17);
		
		IsPagable = new Button(this, SWT.CHECK);
		IsPagable.setText("含页号和页行数");
		IsPagable.setBounds(366, 55, 115, 17);

	}

	public MethodMencacheXml getMethodMencacheXml() throws Exception {
		String cached = "false";
		if (IsCached.getSelection()) {
			cached = "true";
		} else {
			cached = "false";
		}
		
		String staticFile="false";
		
		
		String timeout = this.Timeout.getText().trim();
		long t=-1;
		try {
			t = Long.parseLong(timeout);
		} catch (Exception e) {
			throw new Exception("缓存设置中缓存时间必须为正整数");
		}
		if("true".equals(cached)&&t<=0){
			throw new Exception("缓存设置中缓存时间必须为正整数");
		}
		String staticFileDays="30";
		try {
			t = Long.parseLong(staticFileDays);
		} catch (Exception e) {
			throw new Exception("缓存设置中磁盘驻留必须为正整数");
		}
		if("true".equals(staticFile)&&t<=0){
			throw new Exception("缓存设置中磁盘驻留必须为正整数");
		}
		
		String[] keys = new String[Keylist.getItemCount()];
		for (int i = 0; i < Keylist.getItemCount(); i++) {
			keys[i] = Keylist.getItem(i);
		}
		String isPirvate = "false";
		if (this.IsPrivate.getSelection()) {
			isPirvate = "true";
		}
		String isPage="false";
		if (this.IsPagable.getSelection()) {
			isPage = "true";
		}
		return new MethodMencacheXml(cached,staticFile, timeout,staticFileDays, keys, isPirvate,isPage);
	}

	public void setMethodMencacheXml(MethodMencacheXml methodMencacheXml) {
		if ("true".equals(methodMencacheXml.getCached())) {
			this.IsCached.setSelection(true);
		} else {
			this.IsCached.setSelection(false);
		}
		
		if ("true".equals(methodMencacheXml.getIsPrivate())) {
			this.IsPrivate.setSelection(true);
		} else {
			this.IsPrivate.setSelection(false);
		}
		if ("true".equals(methodMencacheXml.getIsPagable())) {
			this.IsPagable.setSelection(true);
		} else {
			this.IsPagable.setSelection(false);
		}
		this.Timeout.setText(methodMencacheXml.getCatcheTimeout());		
		Keylist.removeAll();
		String[] keys = methodMencacheXml.getKeys();
		if (keys != null) {
			for (int i = 0; i < keys.length; i++) {
				Keylist.add(keys[i]);
			}
		}
	}
	public void setUpdateDisnable(){
		IsCached.setSelection(false);
	}

	public void refreshParms(java.util.List<String> parmKeys) {
		SrcParmlist.removeAll();
		for (int i = 0; i < parmKeys.size(); i++) {
			SrcParmlist.add(parmKeys.get(i));
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
